<template>
  <div class="dashboard">
    <div class="container">
      <!--左侧内容-->
      <div class="left">
        <!--基本信息-->
        <div class="panel">
          <!--个人信息-->
          <div class="user-info">
            <img v-if="avatar" :src="avatar" class="avatar" alt="">
            <span class="username" v-else>{{ name?.charAt(0) }}</span>
            <div class="company-info">
              <div class="title">
                江苏传智博客教育科技股份有限公司
                <span>体验版</span>
              </div>
              <div class="depart">{{ name }}|{{ company }}-{{ departmentName }}</div>
            </div>
          </div>
          <!--待办-->
          <div class="todo-list">
            <div class="todo-item">
              <span>组织总人数</span>
              <!--起始值，终点值，滚动时间-->
              <count-to
                :start-val="0"
                :end-val="homeData.employeeTotal"
                :duration="1000"
              />
            </div>
            <div class="todo-item">
              <span>正式人数</span>
              <count-to
                :start-val="0"
                :end-val="homeData.regularEmployeeTotal"
                :duration="1000"
              />
            </div>
            <div class="todo-item">
              <span>合同待签署</span>
              <count-to
                :start-val="0"
                :end-val="homeData.contractSignTotal"
                :duration="1000"
              />
            </div>
            <div class="todo-item">
              <span>待入职</span>
              <count-to
                :start-val="0"
                :end-val="homeData.toBeEmployed"
                :duration="1000"
              />
            </div>
            <div class="todo-item">
              <span>本月待转正</span>
              <count-to
                :start-val="0"
                :end-val="homeData.toBeConfirmed"
                :duration="1000"
              />
            </div>
            <div class="todo-item">
              <span>本月待离职</span>
              <count-to
                :start-val="0"
                :end-val="homeData.toBeDismissed"
                :duration="1000"
              />
            </div>
            <div class="todo-item">
              <span>接口总访问</span>
              <count-to
                :start-val="0"
                :end-val="homeData.interfaceAccessTotal"
                :duration="1000"
              />
            </div>

          </div>
        </div>
        <!--快捷入口-->
        <div class="panel">
          <div class="panel-title">快捷入口</div>
          <div class="quick-entry">
            <div class="entry-item">
              <div class="entry-icon approval"/>
              <span>假期审批</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon social"/>
              <span>社保管理</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon role"/>
              <span>角色管理</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon salary"/>
              <span>薪资设置</span>
            </div>
            <div class="entry-item">
              <div class="entry-icon bpm"/>
              <span>流程设置</span>
            </div>
          </div>
        </div>
        <!--社保申报数据（）-->
        <div class="panel">
          <div class="panel-title">社保申报数据</div>
          <div class="declare-data">
            <div class="declare-info">
              <div class="info-main">
                <span>申报人数</span>
                <!--
                因为第一个homeData是一个空对象，不存在  declarationTotal这个属性
                存在多层是，如homeData是一个空对象，会报错，此时可以使用的一个？（可选链）
                -->
                <count-to
                  :start-val="0"
                  :end-val="homeData.socialInsurance?.declarationTotal"
                  :duration="1000"
                />
              </div>
              <div class="info-list">
                <div class="info-list-item">
                  <span>待申报(人)</span>
                  <count-to
                    :start-val="0"
                    :end-val="homeData.socialInsurance?.toDeclareTotal"
                    :duration="1000"
                  />
                </div>
                <div class="info-list-item">
                  <span>申报中(人)</span>
                  <count-to
                    :start-val="0"
                    :end-val="homeData.socialInsurance?.declaringTotal"
                    :duration="1000"
                  />
                </div>
                <div class="info-list-item">
                  <span>已申报(人)</span>
                  <count-to
                    :start-val="0"
                    :end-val="homeData.socialInsurance?.declaredTotal"
                    :duration="1000"
                  />
                </div>
              </div>
            </div>

            <div class="declare-chart">
              <!--图表-->
              <!-- 1.为 ECharts 准备一个定义了宽高的 DOM -->
              <div ref="socialChart" style="width: 100%;height: 100%"></div>
            </div>
          </div>
        </div>
        <!--公积金申报数据，图标-->
        <div class="panel">
          <div class="panel-title">公积金申报数据</div>
          <div class="declare-data">
            <div class="declare-info">
              <div class="info-main">
                <span>申报人数</span>
                <count-to
                  :start-val="0"
                  :end-val="homeData.providentFund?.declarationTotal"
                  :duration="1000"
                />
              </div>
              <div class="info-list">
                <div class="info-list-item">
                  <span>待申报(人)</span>
                  <count-to
                    :start-val="0"
                    :end-val="homeData.providentFund?.toDeclareTotal"
                    :duration="1000"
                  />
                </div>
                <div class="info-list-item">
                  <span>申报中(人)</span>
                  <count-to
                    :start-val="0"
                    :end-val="homeData.providentFund?.declaringTotal"
                    :duration="1000"
                  />
                </div>
                <div class="info-list-item">
                  <span>已申报(人)</span>
                  <count-to
                    :start-val="0"
                    :end-val="homeData.providentFund?.declaredTotal"
                    :duration="1000"
                  />
                </div>
              </div>
            </div>

            <div class="declare-chart">
              <div ref="providentChart" style="width: 100%;height: 100%"></div>
            </div>
          </div>
        </div>
      </div>
      <!--右侧内容-->
      <div class="right">
        <!--帮助连接-->
        <div class="panel">
          <div class="help">
            <div class="help-left">
              <div class="panel-title">帮助链接</div>
              <div class="help-list">
                <div class="help-block">
                  <i class="icon-entry"/>
                  入门指南
                </div>
                <div class="help-block">
                  <i class="icon-help"/>
                  在线帮助手册
                </div>
                <div class="help-block">
                  <i class="icon-support"/>
                  联系技术支持
                </div>
                <div class="help-block">
                  <i class="icon-add"/>
                  添加链接
                </div>
              </div>
            </div>
            <div class="help-right">
              <div class="calendar">
                <el-calendar v-model="currentDate">
                </el-calendar>
              </div>
            </div>
          </div>
        </div>
        <!--通知公告-->
        <div class="panel">
          <div class="panel-title">通知公告</div>
          <div class="information-list">
            <div class="information-list-item" v-for="(item,index) in noticeList" :key="index">
              <img :src="item.icon" alt="">
              <div>
                <p>
                  <span class="name">{{ extractName(item.notice) }}</span>
                  发布了<span class="action">{{ extractContent(item.notice) }}</span>
                </p>
                <p>{{ item.createTime }}</p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {mapGetters} from 'vuex'
import CountTo from 'vue-count-to'
import {getHomeData, getHomeNotice} from "@/api/home";
// echartd的引用
// 引入 echarts 核心模块，核心模块提供了 echarts 使用必须要的接口。
import * as echarts from 'echarts/core';
import { LineChart } from 'echarts/charts';// 引入折线图
import {GridComponent} from "echarts/components";// 引入使用组件
import {CanvasRenderer} from "echarts/renderers";// 引入渲染期
//注册所有引用
echarts.use([
  LineChart,
  GridComponent,
  CanvasRenderer
])
export default {
  name: 'Dashboard',
  data() {
    return {
      currentDate: new Date(),
      homeData: {},//首页数据
      noticeList: [],//通知列表
    }
  },
  components: {
    CountTo
  },
  created() {
    this.getHomeData()
    this.getHomeNotice()
  },
  mounted() {
    // 获取展示的数据 设置给图表
    // 监听homeData的变化
    // 初始化
    // data中没有声明 不是响应式
    this.social= echarts.init(this.$refs.socialChart);
    this.provident= echarts.init(this.$refs.providentChart);

  },
  methods: {
    async getHomeData() {
      this.homeData = await getHomeData()
    },
    async getHomeNotice() {
      this.noticeList = await getHomeNotice()
    }

  },
  computed: {
    ...mapGetters([
      'name',
      'avatar',
      'company',
      'departmentName'
    ]),
    extractName() {
      return function (notice) {
        const regex = /^(.*?)发布了/;
        const result = notice.match(regex);
        return result ? result[1] : '';
      };
    },
    extractContent() {
      return function (notice) {
        const regex = /发布了(.+)/
        const result = notice.match(regex);
        return result ? result[1] : '';
      };
    }
  },
  watch:{
    homeData(){
      // 指定图表的配置项和数据
      var socialOption = {
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.homeData.socialInsurance?.xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.homeData.socialInsurance?.yAxis,
            type: 'line',
            areaStyle: {
              color: '#04c9be',
              opacity: 0.2
            },
            lineStyle:{
              color: '#04c9be',
              opacity: 0.3
            },

            showSymbol:false

          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      this.social.setOption(socialOption);
      var providentOption = {
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: this.homeData.providentFund?.xAxis
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: this.homeData.providentFund?.yAxis,
            type: 'line',
            areaStyle: {
              color: '#04c9be',
              opacity: 0.2
            },
            lineStyle:{
              color: '#04c9be',
              opacity: 0.3
            },

            showSymbol:false

          }
        ]
      };

      // 使用刚指定的配置项和数据显示图表。
      this.provident.setOption(providentOption);

    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  background: #f5f6f8;
  width: 100%;
  min-height: calc(100vh-80px);

  .container {
    display: flex;
    gap: 10px;

    .right {
      width: 40%;

      .panel {
        margin-left: 8px;
      }
    }

    .left {
      flex: 1;

      :nth-child(1) {
        margin-top: 0;
      }
    }

    //面板样式
    .panel {
      background-color: #fff;
      margin-top: 8px;
      padding: 20px;
      /*左侧内容*/

      //  用户信息
      .user-info {
        display: flex;

        .avatar {
          width: 48px;
          height: 48px;
          border-radius: 12px;
          line-height: 48px;
          text-align: center;
          background-color: #d9d9d9;
        }

        .username {
          width: 30px;
          height: 30px;
          line-height: 30px;
          text-align: center;
          background-color: #04c9bc;
          color: #fff;
          border-radius: 50%;
          margin-right: 4px;
        }

        .company-info {
          margin-left: 10px;
          height: 48px;
          display: flex;
          flex-direction: column;
          justify-content: space-around;

          .title {
            font-size: 16px;
            font-width: 500px;
            color: #383C4E;
            font-family: PingFang SC, PingFang SC-Medium;

            span {
              font-size: 12px;
              background: #f5f6f8;
              text-align: center;
              padding: 2px 8px;
              border-radius: 2px;
              color: #697086;
            }
          }

          .depart {
            font-size: 14px;
            color: #697086;
            font-weight: 400;
          }
        }
      }

      // 待办
      .todo-list {
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;

        .todo-item {
          display: flex;
          flex-direction: column;
          justify-content: space-around;
          width: 18%;
          height: 90px;
          padding: 10px;

          :nth-child(1) {
            color: #697086;
            font-size: 14px;
          }

          :nth-child(2) {
            color: #383c4e;
            font-size: 30px;
            font-weight: 500;
          }


        }
      }

      /**
      2. 面板标题
       */
      .panel-title {

      }

      /**
      快捷入口
       */
      // 快捷入口
      .quick-entry {
        margin-top: 16px;
        display: flex;
        justify-content: space-around;

        .entry-item {
          display: flex;
          flex-direction: column;
          align-items: center;
          margin-left: 60px;

          &:nth-child(1) {
            margin-left: 0px;
          }

          .entry-icon {
            width: 40px;
            height: 40px;
            border-radius: 10px;
            background: #f5f6f8;
            background-size: cover;


            &.approval {
              background-image: url('~@/assets/common/approval.png');
            }

            &.social {
              background-image: url('~@/assets/common/social.png');
            }

            &.salary {
              background-image: url('~@/assets/common/salary.png');
            }

            &.role {
              background-image: url('~@/assets/common/role.png');
            }

            &.bpm {
              background-image: url('~@/assets/common/bpm.png');
            }
          }

          span {
            color: #697086;
            font-size: 14px;
            margin-top: 8px;
          }
        }

      }

      /**
      申报数据
       */

      .declare-data {
        display: flex;
        gap: 20px;
        //  左侧内容
        .declare-info {
          width: 240px;
          margin-top: 10px;

          .info-main {
            display: flex;
            flex-direction: column;
            padding: 10px;

            :nth-child(1) {
              font-size: 14px;
              color: #697086;
            }

            :nth-child(2) {
              margin-top: 10px;
              font-size: 30px;
              color: #04c9be;
              font-weight: 500;
            }
          }

          .info-list {
            background: #f5f6f8;
            border-radius: 4px;
            padding: 12px 15px;
            display: flex;
            flex-wrap: wrap;
            flex-direction: row;
            align-items: center;

            .info-list-item {
              width: 50%;
              margin-top: 10px;
              display: flex;
              flex-direction: column;
              flex-wrap: wrap;

              :nth-child(1) {
                color: #697086;
                font-size: 14px;
              }

              :nth-child(2) {
                color: #383c4e;
                font-size: 30px;
                font-weight: 500;
              }

            }
          }

        }

        // 右侧图表
        .declare-chart {
          //border: 1px solid #ccc;
          flex: 1

        }
      }

      // 右侧内容
      /**
      帮助链接
      */
      .help {
        display: flex;

        gap: 10px;

        .help-left {
          width: 40%;


          .help-list {
            .help-block {
              background: #f5f6f8;
              border-radius: 4px;
              width: 264px;
              height: 54px;
              padding: 17px 10px;
              margin-top: 10px;
              font-size: 14px;
              color: #697086;

              i {
                width: 14px;
                height: 14px;
                //background: #ccc;
                display: inline-block;
                vertical-align: middle;
                background-size: cover;
              }

              i.icon-help {
                background-image: url("~@/assets/common/help.png");
              }

              i.icon-support {
                background-image: url("~@/assets/common/support.png");
              }

              i.icon-add {
                background-image: url("~@/assets/common/add.png");
              }

              i.icon-entry {
                background-image: url("~@/assets/common/entry.png");
              }
            }
          }
        }

        /*
       日历
       */
        .help-right {
          flex: 1;

          .calendar {
            //修改日历组组件
            ::v-deep .el-calendar-day {
              height: 40px;
            }

            ::v-deep .el-calendar-table__row td, ::v-deep .el-calendar-table tr td:first-child, ::v-deep .el-calendar-table__row td.prev {
              border: none;
            }

            //::v-deep .el-calendar-table td.is-selected .text{
            //  background: #409eff;
            //  color: red;
            //  border-radius: 50%;
            //}
            ::v-deep .el-calendar__button-group {
              display: none;
            }
          }
        }
      }


      /*
      公告*/
      .information-list {
        margin-top: 20px;

        .information-list-item {
          display: flex;
          gap: 10px;
          //align-items: center;/
          margin: 15px 0;
          border-bottom: 1px solid #f5f6f8;

          :nth-child(1) {
            margin-top: 0px;
            font-size: 14px;
            line-height: 22px;

            .name {
              color: #276ffb;
            }

            .action {
              color: #000;
              //color:red;
            }
          }

          :nth-child(2) {
            margin-top: 0;
            margin-bottom: 15px;
            font-size: 14px;
            line-height: 20px;
            color: #c2c2c2;
          }

          img {
            width: 28px;
            height: 28px;
            border-radius: 50%;
          }

        }
      }
    }
  }
}
</style>
